package com.yihen.service.impl;

import com.yihen.dto.BillDto;
import com.yihen.dto.Result;
import com.yihen.mappers.BillMapper;
import com.yihen.pojo.Bill;
import com.yihen.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.Constants;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class BillServiceImpl implements BillService {
    @Autowired
    private BillMapper billMapper;

    @Override
    public Result getAllBills() {
        List<Bill> allBills = billMapper.queryAll();
        return new Result(allBills);
    }

    @Override
    public Result getBillById(long id) {
        BillDto bill = billMapper.queryById(id);
        return new Result(bill);
    }

    @Override
    public Result getBillByDoctorId(long id) {
        List<BillDto> bills = billMapper.queryByDoctorId(id);
        return new Result(bills);
    }

    @Override
    public Result getBillByPatientId(long id) {
        List<BillDto> bills = billMapper.queryByPatientId(id);
        return new Result(bills);
    }

    @Override
    public Result getAllBillDto() {
        List<BillDto> billDtos = billMapper.queryAllBillDto();
        return new Result(billDtos);
    }

    @Override
    public Result getBillsByCondition(BillDto dto) {
        if (dto.getTime() != null) {
            SimpleDateFormat format = new SimpleDateFormat("Y-M-d");
            String timeFormat = format.format(dto.getTime());
            dto.setTimeFormat(timeFormat);
        }
        List<BillDto> billDtos = billMapper.queryBillDtoByCondition(dto);
        return new Result(billDtos);
    }

    @Override
    public Result addBill(Bill bill) {
        try {
            int tag = billMapper.insert(bill);
        } catch (Exception e) {
            // 添加失败，返回失败信息
            return new Result(Constants.ADDFAILED);
        }
        // 添加成功，返回成功信息
        return new Result(null, true, Constants.ADDSUCCESS);
    }
}
